package com.lgy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lgy.dao.CarDao;
import com.lgy.dao.CarorderDao;
import com.lgy.dao.RepertoryDao;
import com.lgy.pojo.Car;
import com.lgy.pojo.CarOrder;
import com.lgy.pojo.Repertory;
import com.lgy.service.ICarOrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class CarOrderServiceImpl implements ICarOrderService {
    @Autowired
    private CarorderDao carorderDao;
    @Autowired
    private RepertoryDao repertoryDao;
    @Autowired
    private CarDao carDao;

    @Override
    public int insertOrder(CarOrder carOrder) {
        return carorderDao.insert(carOrder);
    }

    @Override
    public PageInfo<CarOrder> findOrderListByCompanyId(Integer companyId, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        PageInfo<CarOrder> pageInfo = new PageInfo<CarOrder>(carorderDao.findOrderListByCompanyId(companyId));
        return pageInfo;
    }

    @Override
    public int updateOrderType(Integer orderId,Integer carId,Integer companyId,Integer repertoryNum) {
        int row = carorderDao.updateOrderType(orderId);
        if(row>0){
            Repertory repertory = repertoryDao.findRepertoryByCarId(carId);
            if(repertory!=null){
                repertoryDao.updateRepertoryNum(carId,repertory.getRepertoryNum()+repertoryNum);
                return 1;
            }else {
                Car car = carDao.findCarPriceById(carId);
                BigDecimal purchasePrice = car.getCarPrice().multiply(new BigDecimal(1.2));
                Repertory rep = new Repertory();
                rep.setCarId(car.getCarId()).setCompanyId(companyId).setPurchasePrice(purchasePrice).setInTime(new Date()).setRepertoryNum(repertoryNum);
                int insert = repertoryDao.insert(rep);
                return insert;
            }
        }
        return 0;
    }
}
